package cn.ffcs.uec.wBStaffInfo.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.raptornuke.portal.theme.ThemeDisplay;
import cn.ffcs.uec.outStaff.manager.UecOutStaffManager;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.staff.manager.UecFamilyManager;
import cn.ffcs.uec.staff.manager.UecTrialManager;
import cn.ffcs.uec.staff.model.UecFamily;
import cn.ffcs.uec.staff.model.UecTrial;
import cn.ffcs.uec.wBStaffInfo.action.bean.WbUecOutStaffFamilyListboxBean;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PlatformUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.util.UomZkUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import cn.ffcs.uom.common.zul.PubUtil;
import lombok.Getter;
import lombok.Setter;

/**
 * @desc  外包自助---人员家庭信息CRUD插件
 * @author BMP
 *
 */
@Controller
@Scope("prototype")
public class WbStaffFamilyComposer extends BasePortletComposer implements
     IPortletInfoProvider {

	private static final long serialVersionUID = -5811398149406451671L;
	private String editFamilyUrl="/pages_nmg/uecOutStaffInfomation/uec_outstaff_family_edit.zul";
	/**
	 * 人员基本信息
	 */
	private UecOutStaffInfo uecOutStaffInfo;
	/**
	 * 对应页面上的bean
	 */
	@Getter
	private WbUecOutStaffFamilyListboxBean bean = new WbUecOutStaffFamilyListboxBean();
	/**
	 * 人员家庭信息
	 */
	private UecFamily uecFamily;
	/**
	 * 翻页查询条件实体
	 */
	private UecFamily familyPaging;


	private List<UecFamily> uecFamilies = new ArrayList<UecFamily>();

	private boolean isOutStaffEntryExt = false;
	

	private UecFamilyManager uecFamilyManager = (UecFamilyManager) ApplicationContextUtil
			.getBean("uecFamilyManager");

	private UecOutStaffManager uecOutStaffInfoManager = (UecOutStaffManager) ApplicationContextUtil
			.getBean("uecOutStaffManager");
	
	private UecTrialManager uecTrialManager = (UecTrialManager) ApplicationContextUtil
			.getBean("uecTrialManager");
	@Getter
	@Setter
	private IPortletInfoProvider portletInfoProvider;
	@Override
	public String getPortletId() {
		return super.getPortletId();
	}

	@Override
	public ThemeDisplay getThemeDisplay() {
		return super.getThemeDisplay();
	}

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		UomZkUtil.autoFitHeight(comp);
		Components.wireVariables(comp, bean);
	}

	/**
	 * 初始化
	 */
	public void onCreate$uecFamilyMainWin() {
		onUecOutStaffFamilyQueryResponse();
	}

	/**
	 * 选择查询记录
	 * 
	 * @throws Exception
	 */
	public void onSelectUecFamilyList() throws Exception {
		if (this.bean.getUecFamilyListbox().getSelectedCount() > 0) {
			uecFamily = (UecFamily) bean.getUecFamilyListbox()
					.getSelectedItem().getValue();
		}
	}

	/**
	 * 人员基本信息功能，抛出事件， 事件监听
	 * 
	 * @param event
	 */
	public void onUecOutStaffFamilyQueryResponse() {
		uecOutStaffInfo=new UecOutStaffInfo();
		if (StrUtil.isNullOrEmpty(PlatformUtil.getCurrentUser())) {
			ZkUtil.showError("当前登录用户不存在!", "错误提示");
			return;
		} else {
			String screenName = PlatformUtil.getCurrentUser().getScreenName();
			uecOutStaffInfo.setPsnCode(screenName.toUpperCase());
		}
		if(uecOutStaffInfo!=null) {
			uecOutStaffInfo=uecOutStaffInfoManager.findUecOutStaffInfo(uecOutStaffInfo);
		}
		if (null != uecOutStaffInfo) {
	        //设置操作按钮，非盟市主管不可操作按钮
	       /* if(PermissionUtil.isNoMainOutStaff()) {
	            this.setButtonValid(false, true, true, true);
	        }else {
	            this.setButtonValid(true, true, true, false);
	        }*/
			uecFamily = new UecFamily();
			uecFamily.setStaffId(uecOutStaffInfo.getOutStaffId());
			uecFamily.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			familyPaging = uecFamily;
			this.onQueryUecFamily();
		}
	}


	/**
	 * 查询按钮
	 */
	public void onQueryUecFamily() {
		this.bean.getUecFamilyListboxPaging().setActivePage(0);
		this.onQueryUecFamilyList();
	}

	/**
	 * 重置按钮
	 */
	public void onResetUecFamily() {
		ListboxUtils.clearListbox(this.bean.getUecFamilyListbox());
		this.bean.getUecFamilyListboxPaging().setTotalSize(1);
	}

	/**
	 * 查看按钮
	 * 
	 * @throws Exception
	 * @throws InterruptedException
	 */
	public void onViewUecFamily() throws Exception, InterruptedException {
		if (uecFamily != null) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("opType", "view");
			map.put("uecFamily", uecFamily);
			map.put("isOutStaffEntryExt", isOutStaffEntryExt);
			Window window = (Window) Executions.createComponents(this.editFamilyUrl, null,
					map);
			window.doModal();
			window.addEventListener("onOK", new EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
					if (event.getData() != null) {
						//
					}
				}
			});
		} else {
			ZkUtil.showError("请选中要查看的记录", "提示信息");
			return;
		}
	}

	/**
	 * 新增按钮
	 * 
	 * @throws Exception
	 * @throws
	 */
	public void onAddUecFamily() throws Exception {
		if (null != uecOutStaffInfo) {
			boolean isOnTrial = isOnTrial();
			if (!isOnTrial) {
				return;
			}
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("opType", "add");
			map.put("uecOutStaffInfo", uecOutStaffInfo);
			Window window = (Window) Executions.createComponents(
					this.editFamilyUrl, null,
					map);
			window.doModal();
			window.addEventListener("onOK", new EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
					if (event.getData() != null) {
						// 新增或修改完成后，列表从数据库更新
						uecFamily = (UecFamily) event.getData();
						uecFamilies.add(uecFamily);
							onQueryUecFamilyList();
							ListModel list = new BindingListModelList(
									uecFamilies, true);
							bean.getUecFamilyListbox().setModel(list);
							bean.getUecFamilyListboxPaging().setTotalSize(1);
							onQueryUecFamily();
						}

					}
			});
		}
	}


	/**
	 * 编辑按钮
	 * 
	 * @throws Exception
	 * @throws InterruptedException
	 */
	public void onEditUecFamily() throws Exception, InterruptedException {
		if (null != uecFamily) {
			boolean isOnTrial = isOnTrial();
			if (!isOnTrial) {
				return;
			}
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("opType", "edit");
			map.put("uecFamily", uecFamily);
			Window window = (Window) Executions.createComponents(
					this.editFamilyUrl, null,
					map);
			window.doModal();
			window.addEventListener("onOK", new EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
					if (event.getData() != null) {
						if (isOutStaffEntryExt) {
							uecFamilies.remove(uecFamily);
						}
						uecFamily = (UecFamily) event.getData();
						// 新增或修改完成后，列表从数据库更新
							onQueryUecFamilyList();
							uecFamilies.add(uecFamily);
							ListModel list = new BindingListModelList(
									uecFamilies, true);
							bean.getUecFamilyListbox().setModel(list);
							bean.getUecFamilyListboxPaging().setTotalSize(1);
							onQueryUecFamily();

					}
				}
			});
		} else {
			ZkUtil.showError("请选中要编辑的记录", "提示信息");
			return;
		}
	}


	/**
	 * 删除按钮
	 */
	public void onDelUecFamily() {
		if (this.uecFamily != null && uecFamily.getId() != null) {
			ZkUtil.showQuestion("确定要删除吗?", "提示信息", new EventListener() {
				public void onEvent(Event event) throws Exception {
					Integer result = (Integer) event.getData();
					if (result == Messagebox.OK) {
						uecFamily.removeOnly();
						PubUtil.reDisplayListbox(bean.getUecFamilyListbox(),
								uecFamily, "del");
						uecFamily = null;
					}
				}
			});
		} else {
			ZkUtil.showError("请选中要删除的记录", "提示信息");
			return;
		}
	}

	/**
	 * 翻页
	 */
	public void onUecOutFamilyListboxPaging() {
		this.onQueryUecFamilyList();
	}

	/**
	 * 清除变量
	 */
	public void clearVariable() {
		uecFamily = null;
		familyPaging = null;
		uecFamilies.clear();
	}

	/**
	 * 查询
	 */
	public void onQueryUecFamilyList() {
		if (null != familyPaging) {
			PageInfo pageInfo = uecFamilyManager.queryPageInfoByUecFamily(
					familyPaging, this.bean.getUecFamilyListboxPaging()
							.getActivePage() + 1, this.bean
							.getUecFamilyListboxPaging().getPageSize());
			ListModel list = new BindingListModelList(pageInfo.getDataList(),
					true);
			this.bean.getUecFamilyListbox().setModel(list);
			this.bean.getUecFamilyListboxPaging().setTotalSize(
					pageInfo.getTotalCount());
		}
	}
	/**
	 * 判断是否入职
	 * 
	 * @return
	 */
	private boolean isOnTrial() {
		UecTrial qryUecTrial = UecTrial.newInstance();
		qryUecTrial.setStaffId(uecOutStaffInfo.getOutStaffId());
		qryUecTrial.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);
		qryUecTrial = uecTrialManager.findUecTrial(qryUecTrial);
		if (null == qryUecTrial) {
			ZkUtil.showError("您未办理入职,请主管执行入职操作", "温馨提示");
			return false;
		}
		return true;
	}

}
